from UserSettingsPage import UserSettingsPage
from Autotests.LocalMachine.Controls.Button import WebButton
from Autotests.LocalMachine.Controls.TableView import WebTableView
from Autotests.LocalMachine.Controls.LineEdit import WebLineEdit
from Autotests.LocalMachine.Controls.ComboBox import WebComboBox
from Autotests.LocalMachine.Controls.BaseControl import WebFindKey, BaseWebControl
from Autotests.LocalMachine.UiObjectsMap.AirManager import UserSettings
from Autotests.WebDriver import DRIVER
import time


class Role(object):
    user = "6"
    superUser = "1"

class AddUserDialog(UserSettingsPage):
    def __init__(self):
        super(AddUserDialog, self).__init__()
        self._txtEmail = WebLineEdit(UserSettings.AddUser.txtEmail)
        self._txtName = WebLineEdit(UserSettings.AddUser.txtName)
        self._txtPassword = WebLineEdit(UserSettings.AddUser.txtPassword)
        self._txtConfirmPassword = WebLineEdit(UserSettings.AddUser.txtConfirmPassword)
        self._comboBoxRole = WebComboBox(UserSettings.AddUser.cbRole)

    def goToHomeLocation(self):
        pass

    def addUser(self, email, name, password, role):
        self._txtEmail.typeText(email)
        self._txtName.typeText(name)
        self._txtPassword.typeText(password)
        self._txtConfirmPassword.typeText(password)
        self._comboBoxRole.selectItemsByDataValue(role)
        WebButton(UserSettings.AddUser.btnAdd).click()


class UserTab(UserSettingsPage):
    def __init__(self):
        super(UserTab, self).__init__()
        self._buttonAddUser = WebButton(UserSettings.User.btnAddUser)
        self._tableUserRoles = WebTableView(UserSettings.User.tableUserRoles)

    def goToHomeLocation(self):
        super(UserTab, self).goToHomeLocation()
        WebButton(UserSettings.btnUser).click()
        self.waitFor(lambda x: not BaseWebControl(UserSettings.rightFrameLoader).isExists(2), timeoutSec=10)

    def viewAddUserDialog(self):
        self.goToHomeLocation()
        time.sleep(1)
        self._buttonAddUser.click()

    @UserSettingsPage.setter
    def addUser(self, email, name, password, role):
        self._buttonAddUser.click()
        AddUserDialog().addUser(email, name, password, role)

    @UserSettingsPage.setter
    def deleteUser(self, email):
        self.makeSettingsDialogVisible(email)
        WebButton(UserSettings.User.deleteUser).click()
        WebButton(UserSettings.DeleteUser.btnDelete).click()

    @UserSettingsPage.setter
    def editUserRole(self, email, role):
        self.makeSettingsDialogVisible(email)
        WebButton(UserSettings.User.editUser).click()
        WebComboBox(UserSettings.EditUser.cbRole).selectItemsByDataValue(role)
        WebButton(UserSettings.EditUser.btnSave).click()

    def makeSettingsDialogVisible(self, item):
        column = 2
        row = [x.text for x in self._tableUserRoles.column(0)].index(item)
        self._tableUserRoles.item(row, column).click()

    @UserSettingsPage.getter
    def getUserList(self):
        result = []
        for item in self._tableUserRoles.column(0):
            result.append(item.text)
        return result

    @UserSettingsPage.getter
    def getUserCount(self):
        return self._tableUserRoles.rowsCount()

    @UserSettingsPage.getter
    def getUserRole(self, email):
        self.makeSettingsDialogVisible(email)
        WebButton(UserSettings.User.editUser).click()
        role = WebComboBox(UserSettings.EditUser.cbRole).currentItem().dataValue()
        WebButton(UserSettings.EditUser.btnCancel).click()
        return role
